﻿using FairyGUI;
using UnityEngine;

public class TestMain : MonoBehaviour
{
    private GComponent _mainView;
    public GButton _btnClose;
    public GObject _btnOK;
    public GObject _btnCancle;
    public GTextField _title;
    public GTextField _msg;


    public void Awake()
    {
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
        //Use the font names directly
        UIConfig.defaultFont = "Microsoft YaHei";
#else
        //Need to put a ttf file into Resources folder. Here is the file name of the ttf file.
		UIConfig.defaultFont = "afont";
#endif
        //Test是我们在FairyGUI Editor工程下创建的Package的名字，此处路径是相对Resources的路径
        UIPackage.AddPackage("UI/Test");
    }

    private void Start()
    {
        Application.targetFrameRate = 60;//帧率设置
        Stage.inst.onKeyDown.Add(OnKeyDown);//返回事件

        _mainView = this.GetComponent<UIPanel>().ui;//当前的UIPanel

        _title = _mainView.GetChild("title").asTextField;//标题
        _msg = _mainView.GetChild("msg").asTextField;//内容

        _btnClose = _mainView.GetChild("button_close").asButton;
        _btnOK = _mainView.GetChild("button_ok");
        _btnCancle = _mainView.GetChild("button_cancle");

        _btnClose.onClick.Add(onBtnCloseClick);
        _btnOK.onClick.Add(onBtnOKClick);
        _btnCancle.onClick.Add(onBtnCancleClick);

        _title.text = "FairyGUI";
    }

    private void onBtnCloseClick()
    {
        Debug.Log("close");
        this.gameObject.SetActive(false);
    }

    private void onBtnOKClick()
    {
        Debug.Log("OK");
        _msg.text = "This is clicked by BtnOk!";
    }

    private void onBtnCancleClick()
    {
        Debug.Log("Cancle");
        _msg.text = "This is clicked by BtnCancle!";

    }

    private void OnKeyDown(EventContext context)
    {
        if (context.inputEvent.keyCode == KeyCode.Escape)
        {
            Application.Quit();
        }
    }
}
